<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/common/taglib.jsp" %>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta content="微社区,Q生活" name="keywords"/>
<meta content="微社区,Q生活" name="description"/>
<title>微社区|Q生活</title>
<link rel="stylesheet" href="${ctx}/static/css/Master.css"/>
<link rel="stylesheet" href="${ctx}/static/css/jquery-labelauty.css"/>
<script src="${ctx}/static/js/jquery-1.9.0.min.js"></script>
<script src="${ctx}/static/js/jquery-labelauty.js"></script>

<link href="${ctx}/static/css/validation.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript" src="${ctx}/static/js/jquery.validate.min.js"></script>
<script type="text/javascript" src="${ctx}/static/js/jquery.metadata.js"></script>
<script type="text/javascript" src="${ctx}/static/js/messages_cn.js"></script>

<script src="${ctx}/static/plugins/layer/layer.js"></script>
<%-- 基础js,所有页面都需要引入此js,依赖layer.js --%>
<script src="${ctx}/static/js/common/base.js"></script>
<script type="text/javascript">
	var form;
	
	$(document).ready(function(){
		form = $("#editForm");
		//单选按钮样式
		$('.labelautybox :input').labelauty();
		radioChange();
		
		//异步验证资源名称是否重复
	    jQuery.validator.addMethod("checkResourceNameExist", function(value, element){
	        var result = false;
	        var resId = $("#resId").val();
	        if(!resId){
	        	resId = 0;
	        }
	        // 设置同步
	        $.ajaxSetup({
	            async: false
	        });
	        $.get("${ctx}/resource/"+resId+"/"+value+"/checkResourceNameExist", function(data){
	        	if(typeof data == "string"){
	        		if(data == "true"){
	        			data = true;
	        		}else{
	        			data = false;
	        		}
	        	}
	            result = !data;
	            
	        });
	        // 恢复异步
	        $.ajaxSetup({
	            async: true
	        });
	        return result;
	    }, "资源名称已存在");
	});
	
	function radioChange(){
		var radio = $("input[name='resType']");
		radio.each(function(i,item){
			if(item.checked){
				//1菜单 2按钮
				if(item.value == 1){
					$("#dis input:text").attr("disabled",false);
					//$("#resUrl").addClass("required");
				}else{
					$("#dis input:text").attr("disabled","disabled");
					//$("#resUrl").removeClass("required");
					//重新验证信息
					form.valid();
					
				}
			}
		});
	}
	
	function submit(){
		if(!form.valid()){
			return false;
		}
		
		$.ajax({    
		    //url:form.attr("action"),//可以不要   
		    data:form.serialize(),    
		    type:"post",    
		    cache:false,    
		    dataType:"json",
		    async:true, //默认为true 异步    
		    success:function(data) {    
		        top.layer.msg(data,{},function(){
		        	window.location="${ctx}/resource/list";   
	            });
		     } 
		});
	}
</script>
</head>

<body>
<section class="boxRight">
<div class="OrgTit">${op }</div>
<div class="context">
	<form:form method="post" commandName="resource" id="editForm" >
	<form:hidden path="resId"/>
    <form:hidden path="resPId"/>
    <form:hidden path="resPIds"/>
    
	<div class="add_firstmenu">
		<c:if test="${not empty parent}">
		<dl>
			<dt>
				父资源名称
			</dt>
			<dd>
				${parent.resName}
			</dd>
		</dl>
	    </c:if>
	    
		<dl>
			<dt>
				<span class="im">*</span><c:if test="${not empty parent}">子</c:if>资源名称
			</dt>
			<dd>
				<form:input class="w1 required checkResourceNameExist" path="resName" maxlength="10"/>
			</dd>
		</dl>
		
		<dl>
			<dt>
				类型
			</dt>
			<dd>
				<div class="labelautybox labelautybox3">
				<c:forEach items="${resourceType}" var="type" varStatus="status">
				<input type="radio" name="resType" data-labelauty="${type.content }" onchange="radioChange()" value="${type.code }" <c:if test="${(empty resource.resType && status.index==0) || (resource.resType == type.code) }">checked="checked"</c:if> >
				</c:forEach>
				</div>
			</dd>
		</dl>
		
		<dl>
			<dt>
				权限字符串
			</dt>
			<dd>
				<form:input class="w1" path="resPermission" maxlength="50"/>
			</dd>
		</dl>
		
		
		<div id="dis">
		<dl>
			<dt>
				菜单URL
			</dt>
			<dd>
				<form:input class="w1" path="resUrl" maxlength="100"/>
			</dd>
		</dl>
		
		<dl>
			<dt>
				菜单顺序
			</dt>
			<dd>
				<form:input class="w1 digits" path="resOrder" maxlength="5"/>
			</dd>
		</dl>
		
		<dl>
			<dt>
				菜单样式
			</dt>
			<dd>
				<form:input class="w1" path="resClass" maxlength="20"/>
			</dd>
		</dl>
		</div>
		<div class="combtn"><a href="javascript:void(0);" onclick="submit();">确定</a><a href="javascript:void(0);" onclick="history.back();">取消</a></div>
	</div>
	</form:form>
</div>
</section>
</body>
</html>
